home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Converters / Convert_PICT / Source / CommentedPSCode / Text < prev   
Text File  |  1995-06-12  |  10KB  |  476 lines

  1. %BEGIN Text
  2.  
  3. % Copyright (C) 1993 David John Burrowes
  4. % Distributed under terms of GNU General Public License.
  5. % See COPYING.text in Convert PICT's CommentedPSCode for a copy
  6.  
  7. %
  8. %    The current text drawing position
  9. %
  10. /textY 0 def
  11. /textX 0 def
  12. %
  13. %    Extra width used when printing spaces
  14. %
  15. /extraSpaceWidth 0 def
  16. /extraCharWidth 0 def
  17. %
  18. %    Establish default font 
  19. %
  20. /typeSize 12 def
  21. /Chicago findfont
  22. typeSize scalefont
  23. setfont
  24. %
  25. %    the x and y scaling factors
  26. %
  27. /xscale 1 def
  28. /yscale 1 def
  29. %
  30. %    Style flags (true eq use that style)
  31. %
  32. /macBold false def
  33. /macItalic false def
  34. /macUnderline false def
  35. /macOutline false def
  36. /macShadow false def
  37. /macCondense false def
  38. /macExtend false def
  39. %
  40. %    Mode
  41. %
  42. /textMode    /srcCopy    def
  43. %
  44. %    For PicComment processing
  45. %
  46. /suppressText false def
  47. /textCenterX 0 def
  48. /textCenterY 0 def
  49. /textAngle 0 def
  50. /rotateText false def
  51.  
  52. %%%%%%%%%%%%%
  53. %    Name:    drawChar
  54. %    Syntax:    string drawChar -
  55. %    About:    Draws specifid string, This should only be called by PICTshow, as it depends on
  56. %            values set up there. 
  57. %%%%%%%%%%%%%
  58. /drawChar
  59. {
  60.     textMatrix setmatrix
  61.     show
  62.     originalMatrix setmatrix
  63. }
  64. def
  65.  
  66. %%%%%%%%%%%%%
  67. %    Name:    pathNonZero
  68. %    Syntax:    - pathNonZero bool
  69. %    About:    returns true if current path has a non-zero height
  70. %%%%%%%%%%%%%
  71. /pathNonZero
  72. {
  73.     pathbbox
  74.     /top exch def
  75.     pop
  76.     /bottom exch def
  77.     pop
  78.     
  79.     top bottom sub  0 eq
  80.         {false}
  81.         {true}
  82.     ifelse
  83. }
  84. def
  85.  
  86. %%%%%%%%%%%%%
  87. %    Name:    PICTshow
  88. %    Syntax:    x y string PICTshow -
  89. %    About:    Draw the specified text at x,y.  theString is drawn one char at a time because
  90. %        this provides better control when drawing the various type styles.
  91. %        The various styles are drawn to emulate on-screen mac drawing.  Thus, italics are
  92. %        obliqued, bold is double-printed (slightly offset).  Condensed and Extended have
  93. %        altered spacing, but characters are not drawn thin or fat.  Etc.  
  94. %    Bugs:
  95. %        Bold outlines and shadows have a doubled left border.
  96. %        Shadowed char extending to *left* will place shadow on neighbor, not behind
  97. %        extend values not well defined on Mac.  I've made good approximations here.
  98. %%%%%%%%%%%%%
  99. /PICTshow
  100. {
  101.     /theString exch def
  102.     /y exch def
  103.     /x exch def
  104.     /extend 0 def        % extra space to widen each char
  105.     /charString 1 string def    % holds char as a string
  106.  
  107.     suppressText false eq
  108.     {
  109.         gsave
  110.             %
  111.             %    Immediately rotate our coordinate space if the rotateText flag is true.
  112.             %    Warning:  The matrix stuff seems to overwite some of this.  ICK.
  113.             %
  114.             rotateText true eq
  115.                 { textAngle rotate }
  116.             if
  117.         
  118.             /originalMatrix matrix currentmatrix def
  119.             %
  120.             %    Build a matrix to use when drawing text: make it upside down (-1) so text will
  121.             %    appear rightside up, slant it (.5) if doing 'italic', and scale by the
  122.             %    scaling factors.  (I dislike this explicit 'putting' of values into the matrix...
  123.             %    it may mess up the image in some cases.  The .5 put seems needed to get
  124.             %    around a NS 3.1 Preview bug though.
  125.             %
  126.              /textMatrix
  127.                 xscale yscale matrix currentmatrix scale
  128.                 dup 3 
  129.                     matrix currentmatrix 3 get
  130.                 -1 mul put
  131.                 macItalic true eq
  132.                     { dup 2 .5 put }
  133.                 if
  134.             def
  135.             %
  136.             %    Store ammount to extend width of each char by
  137.             %
  138.             macBold true eq
  139.                 {/extend  extend 1.5 add def }
  140.             if
  141.             
  142.             macOutline true eq
  143.                 { /extend  extend 2 add def }
  144.             if
  145.         
  146.             macExtend true eq
  147.                 {/extend  extend 2 add def }
  148.             if
  149.         
  150.             macShadow true eq
  151.                 {/extend  extend 2 add def }
  152.             if
  153.         
  154.             macCondense true eq
  155.                 {/extend  extend 2 sub def }
  156.             if
  157.             %
  158.             %    For each character in string, draw it...
  159.             %
  160.             foregroundColor    useColor
  161.             theString
  162.             {
  163.                 %
  164.                 %    Define next char of theString, width of the char, and extra space
  165.                 %    to add to this character.
  166.                 %
  167.                 /theChar exch def
  168.                 charString 0 theChar put
  169.                 /charWidth charString stringwidth pop def % pop y dimen
  170.                 32 theChar eq
  171.                     { /extraSpace extraSpaceWidth def }
  172.                     { /extraSpace extraCharWidth def }
  173.                 ifelse
  174.                 %
  175.                 %    Draw a shadow
  176.                 %
  177.                 macShadow true eq
  178.                 {
  179.                     x 2 add y 2 add moveto % move down & right to draw shadow
  180.                     charString drawChar
  181.                     macBold true eq
  182.                     {
  183.                         x 3 add y 2 add moveto
  184.                         charString drawChar
  185.                     }
  186.                     if
  187.                 }
  188.                 if
  189.                 %
  190.                 %    Draw other text.  If doing bold, loop twice, incrementing boldStep
  191.                 %    second time to produce the double-print of bold.
  192.                 %
  193.                 /boldstep 0 def
  194.                 macBold true eq {2} {1} ifelse    % loop 2ce if bold is on.
  195.                 {
  196.                     %
  197.                     %    If doing shadow or outline, draw the text in white with black outline
  198.                     %    unless path has no height (maybe it's a bitmap), draw the char normal
  199.                     %
  200.                     macShadow true eq   macOutline true eq  or
  201.                     {
  202.                         newpath
  203.                         x boldstep add y moveto
  204.                         textMatrix setmatrix
  205.                         charString true charpath
  206.                         originalMatrix setmatrix
  207.                         pathNonZero true eq
  208.                         {
  209.                             1 setgray
  210.                             fill
  211.                             foregroundColor    useColor
  212.                             
  213.                             newpath
  214.                             x boldstep add y moveto
  215.                             textMatrix setmatrix
  216.                             charString false charpath
  217.                             originalMatrix setmatrix
  218.                             0 setlinewidth
  219.                             stroke
  220.                         }
  221.                         {
  222.                             x boldstep add y moveto
  223.                             charString drawChar
  224.                         }
  225.                         ifelse
  226.                     }
  227.                     { 
  228.                         x boldstep add y moveto
  229.                         charString drawChar
  230.                     }
  231.                     ifelse
  232.                     /boldstep boldstep 1 add def
  233.                 }
  234.                 repeat
  235.                 originalMatrix setmatrix
  236.         
  237.                 macUnderline true eq
  238.                 {
  239.                     x y 1 add moveto
  240.                     x charWidth add extraSpace add extend add y 1 add lineto
  241.                     1 setlinewidth
  242.                     stroke
  243.                 }
  244.                 if
  245.                 %
  246.                 %    Increment x for the next character's start
  247.                 %
  248.                 /x x charWidth add extraSpace extend add add def
  249.             }
  250.             forall
  251.         grestore
  252.     }
  253.     if    % if we were to suppress writing text out.
  254. }
  255. def
  256.  
  257. %%%%%%%%%%%%%
  258. %    Name:    txFont         [0003]
  259. %    Syntax:    name txFont -
  260. %            num txFont -
  261. %    About:    This sets the type family to draw text in. If param is not a
  262. %            name, the converter could not identify the `font', so this
  263. %            routine must decide what family to use.
  264. %%%%%%%%%%%%%
  265. /txFont
  266. {
  267.     /family exch def
  268.  
  269.     family type /nametype eq
  270.     {
  271.         family findfont
  272.     }
  273.     {
  274.         %
  275.         %    Convert the number to a font name
  276.         %
  277.         family 134 eq
  278.             {/Linguistics findfont}
  279.             {family 149 eq
  280.                 {/NewOrleans findfont}
  281.                 {family 64 eq
  282.                     {/Davids findfont}
  283.                     {/Chicago findfont}   % default font
  284.                 ifelse}
  285.             ifelse}
  286.         ifelse
  287.     }
  288.     ifelse
  289.     
  290.     typeSize scalefont
  291.     setfont
  292. }
  293. def
  294.  
  295. %%%%%%%%%%%%%
  296. %    Name:    txFace         [0004]
  297. %    Syntax:    array txFace -
  298. %    About:    Given an array of names, set several global flags to based on
  299. %            them.  These indicate what text styles to use when drawing text.
  300. %%%%%%%%%%%%%
  301. /txFace
  302. {
  303.     /styleArray exch def
  304.     /macBold false def
  305.     /macItalic false def
  306.     /macUnderline false def
  307.     /macOutline false def
  308.     /macShadow false def
  309.     /macCondense false def
  310.     /macExtend false def
  311.     styleArray
  312.     {
  313.         /style exch def
  314.         style /bold eq {/macBold true def}
  315.         {style /italic eq {/macItalic true def}
  316.         {style /underline eq {/macUnderline true def}
  317.         {style /outline eq {/macOutline true def}
  318.         {style /shadow eq {/macShadow true def}
  319.         {style /condense eq {/macCondense true def}
  320.         {style /extend eq {/macExtend true def}
  321.         {    % Actually, this is an error condition of some sort
  322.         } ifelse
  323.         } ifelse
  324.         } ifelse
  325.         } ifelse
  326.         } ifelse
  327.         } ifelse    
  328.         } ifelse    
  329.     }
  330.     forall
  331. }
  332. def
  333.  
  334. %%%%%%%%%%%%%
  335. %    Name:    txMode         [0005]
  336. %    Syntax:    name txMode -
  337. %    About:    Sets the text drawing mode passed by caller.  This is not used
  338. %            so we just store it.
  339. %            Note: if mode was unknown, we get /badMode-# (# is mode num))
  340. %%%%%%%%%%%%%
  341. /txMode
  342.     { /textMode exch def }
  343. def
  344.  
  345. %%%%%%%%%%%%%
  346. %    Name:    spExtra     [000C]
  347. %    Syntax:    num spExtra  -
  348. %    About:    Changes width of space character in future text drawing
  349. %            operations.
  350. %%%%%%%%%%%%%
  351. /spExtra
  352.     {/extraSpaceWidth exch def }
  353. def
  354.  
  355. %%%%%%%%%%%%%
  356. %    Name:    txSize        [000D]
  357. %    Syntax:    num txSize -
  358. %    About:    Sets the size of subsequent text to the passed number.
  359. %            Do this by scaling relative to the current size.
  360. %%%%%%%%%%%%%
  361. /txSize
  362. {
  363.     /newTypeSize exch def
  364.     currentfont
  365.         newTypeSize typeSize div scalefont
  366.     setfont
  367.     /typeSize newTypeSize def
  368. }
  369. def
  370.  
  371. %%%%%%%%%%%%%
  372. %    Name:    TxRatio        [0010]
  373. %    Syntax:    xscale yscale txRatio -
  374. %    About:    This sets a horozontal and vertical scaling factors for text
  375. %            that is subsequently drawn.
  376. %%%%%%%%%%%%%
  377. /txRatio
  378. {
  379.     /yscale exch def
  380.     /xscale exch def
  381. }
  382. def
  383.  
  384. %%%%%%%%%%%%%
  385. %    Name:    chExtra        [0016]
  386. %    Syntax:    num chExtra -
  387. %    About:    The chExtra opcode probably sets the spacing between characters.
  388. %            I couldn't confirm this, though, so this does nothing.
  389. %%%%%%%%%%%%%
  390. /chExtra
  391.     {pop}
  392. def
  393.  
  394. %%%%%%%%%%%%%
  395. %    Name:    longText        [0028]
  396. %    Syntax:    num num string  longText -
  397. %    About:    Displays the string at coords. Makes coords the new textX,Y
  398. %%%%%%%%%%%%%
  399. /longText
  400. {
  401.     /theString exch def
  402.     /textY exch def
  403.     /textX exch def
  404.     
  405.     textX textY  theString     PICTshow
  406. }
  407. def
  408.  
  409. %%%%%%%%%%%%%
  410. %    Name:    DHText        [0029]
  411. %    Syntax:    num string  DHText -
  412. %    About:    Displays string offset sideways from textX (positive is right)
  413. %%%%%%%%%%%%%
  414. /DHText
  415. {
  416.     /theString exch def
  417.     /deltaX exch def
  418.     
  419.     deltaX textX add textY theString longText
  420. }
  421. def
  422.  
  423. %%%%%%%%%%%%%
  424. %    Name:    DVText        [002A]
  425. %    Syntax:    num string  DVText -
  426. %    About:    Displays string offset down from textY (positive is down)
  427. %%%%%%%%%%%%%
  428. /DVText
  429. {
  430.     /theString exch def
  431.     /deltaY exch def
  432.     textX deltaY textY add theString longText
  433. }
  434. def
  435.  
  436. %%%%%%%%%%%%%
  437. %    Name:    DHDVText        [002B]
  438. %    Syntax:    num num string  DVText -
  439. %    About:    Displays string offset from text X,Y (positive is down & right)
  440. %%%%%%%%%%%%%
  441. /DHDVText
  442. {
  443.     /theString exch def
  444.     /deltaY exch def
  445.     /deltaX exch def
  446.     
  447.     deltaX textX add   deltaY textY add  theString longText
  448. }
  449. def
  450.  
  451. %%%%%%%%%%%%%
  452. %    Name:    fontName         [002C]
  453. %    Syntax:    name fontName -
  454. %    About:    Given the name of a type font, this calls txFont to actually process
  455. %            the font name.  
  456. %%%%%%%%%%%%%
  457. /fontName
  458.     { txFont }
  459. def
  460.  
  461.  
  462. %%%%%%%%%%%%%
  463. %    Name:    lineJustify         [002D]
  464. %    Syntax:    num num lineJustify  -
  465. %    About:    Changes the extra width for normal characters (no spaces), and some
  466. %            presently unknown value.
  467. %%%%%%%%%%%%%
  468. /lineJustify
  469. {
  470.     pop
  471.     /extraCharWidth exch def 
  472. }
  473. def
  474.  
  475. %END Text
  476.